/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlSource.java
 *
 * Created on Aug 11, 2010, 4:46:14 PM
 */

package Gui;

import BusinessLogic.BackGroundUtility;
import BusinessLogic.DateUtility;
import BusinessLogic.ProductManagement;
import DTO.Exhibitor;
import DTO.Product;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class DLProduct extends javax.swing.JDialog implements MouseListener{

    private ProductManagement ProductMag;
    private Vector<Exhibitor> listExhibitor;
    private Vector<Product> listProduct;
    private String nameExhibitor;
    private JPanel[] product;

    /** Creates new form dlSource */
    public DLProduct(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //set background
        ImagePanel iPanel = new ImagePanel(BackGroundUtility.getImage());
        getContentPane().add(iPanel);
        //setlocation for application
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        Dimension dimApp = getSize();
        int pointX = (dim.width - dimApp.width)/2;
        int pointY = (dim.height - dimApp.height)/2 - 10;
        setLocation(pointX, pointY);
        //Create object Booth management
        ProductMag = new ProductManagement();
        
        //call method load list ImportantDate
        listExhibitor = ProductMag.loadExhibitor();
        displayExhibitor();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnBackground = new javax.swing.JPanel();
        pnContent = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtDateCreate = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        srDecription = new javax.swing.JScrollPane();
        txtDecription = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        txtSearch = new javax.swing.JTextField();
        pnProduct = new javax.swing.JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Product Management");

        pnBackground.setBackground(new java.awt.Color(0, 102, 102));
        pnBackground.setOpaque(false);

        pnContent.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setLayout(null);

        jLabel4.setText("ID");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(20, 20, 20, 14);

        txtID.setEditable(false);
        txtID.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(txtID);
        txtID.setBounds(50, 20, 80, 20);

        txtName.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(txtName);
        txtName.setBounds(50, 50, 140, 20);

        jLabel2.setText("Name");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 50, 40, 14);

        jLabel12.setText("Date Create");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(190, 20, 70, 14);

        txtDateCreate.setEditable(false);
        txtDateCreate.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.add(txtDateCreate);
        txtDateCreate.setBounds(270, 20, 200, 20);

        jLabel6.setText("Decription");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(200, 50, 48, 14);

        srDecription.setBorder(null);
        srDecription.setViewportBorder(javax.swing.BorderFactory.createEtchedBorder());
        srDecription.setViewportView(txtDecription);

        jPanel1.add(srDecription);
        srDecription.setBounds(270, 50, 200, 70);

        jPanel2.setLayout(null);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel2.add(btnSave);
        btnSave.setBounds(60, 10, 70, 23);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel2.add(btnAdd);
        btnAdd.setBounds(150, 10, 70, 23);

        jButton7.setText("Del");
        jPanel2.add(jButton7);
        jButton7.setBounds(240, 10, 70, 23);

        jButton8.setText("Cancel");
        jPanel2.add(jButton8);
        jButton8.setBounds(330, 10, 70, 23);

        jPanel3.setLayout(null);

        jLabel3.setText("Look for:");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(10, 0, 70, 20);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jList1MousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(jList1);

        jPanel3.add(jScrollPane2);
        jScrollPane2.setBounds(10, 50, 150, 350);

        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });
        jPanel3.add(txtSearch);
        txtSearch.setBounds(10, 20, 150, 19);

        pnProduct.setBorder(javax.swing.BorderFactory.createTitledBorder("List Product"));

        javax.swing.GroupLayout pnContentLayout = new javax.swing.GroupLayout(pnContent);
        pnContent.setLayout(pnContentLayout);
        pnContentLayout.setHorizontalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                    .addComponent(pnProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnContentLayout.setVerticalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                        .addComponent(pnProduct, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout pnBackgroundLayout = new javax.swing.GroupLayout(pnBackground);
        pnBackground.setLayout(pnBackgroundLayout);
        pnBackgroundLayout.setHorizontalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnBackgroundLayout.setVerticalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(pnBackground, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        //gettext from textfiels
        if(checkEmpty() == false){
            JOptionPane.showMessageDialog(this, "a TextField is Empty!!!");
            return;
        }
        String id = txtID.getText();
        String name = txtName.getText();
        String dateCreate = txtDateCreate.getText();
        String decription = txtDecription.getText();
        String idExhibitor = ProductMag.getID(nameExhibitor);
        System.out.println(idExhibitor);

        Product p = new Product(id, idExhibitor, name, dateCreate, decription);
        ProductMag.insert(p);

        //call load listProduct again of this Exhibitor
        listProduct(nameExhibitor);
}//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        //call method clearText()
        clearText();
        //method createID automatic create new id
        String id = ProductMag.createID();
        txtID.setText(id);
        //call method get today
        String today = DateUtility.today();
        txtDateCreate.setText(today);
}//GEN-LAST:event_btnAddActionPerformed

    private void jList1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MousePressed
        // TODO add your handling code here:
        nameExhibitor = (String)jList1.getSelectedValue();
        txtSearch.setText(nameExhibitor);
        listProduct(nameExhibitor);
}//GEN-LAST:event_jList1MousePressed

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        // TODO add your handling code here:
        String name = txtSearch.getText();
        listExhibitor = ProductMag.filterExhibitor(name);
        //display exhibitor by new list
        displayExhibitor();
}//GEN-LAST:event_txtSearchKeyReleased

    public void listProduct(String nameExhibitor){
        pnProduct.removeAll();
        pnProduct.updateUI();
        
        listProduct = ProductMag.loadProduct(nameExhibitor);
        int numberProduct = listProduct.size();
        product = new ImagePanel[numberProduct];
 
        for (int i = 0; i < numberProduct; i++) {
            Product pro = listProduct.get(i);

            JLabel name = new JLabel(pro.getName());
            product[i] = new ImagePanel("image/product.jpg");
            product[i].setLayout(null);
            product[i].setPreferredSize(new Dimension(100, 100));
            product[i].setBorder(javax.swing.BorderFactory.createLineBorder(Color.lightGray));
            product[i].add(name);
            name.setBounds(20, 80, 50, 20);
            product[i].addMouseListener(this);
            
            pnProduct.add(product[i]);
        }
        //call method display Product
        if(numberProduct > 0){
            displayObject(0);
        }    
        else{
            clearText();
        }
    }
    public void displayExhibitor(){ 
       Vector<String> listName = new Vector<String>();
       
       for(Exhibitor ex : listExhibitor){
           listName.add(ex.getName());
       }
       jList1.setListData(listName);
       jList1.setSelectedIndex(0);
       //load product of this Exhibitor
       if(listName.size() > 0){
           listProduct(listName.get(0));
       }
    }

    private void displayObject(int index){
        Product p = ProductMag.getByIndex(index);

        //set values for textfiels
        txtDateCreate.setText(p.getDateCreate());
        txtDecription.setText(p.getDecription());
        txtID.setText(p.getId());
        txtName.setText(p.getName());
    }

    private void clearText(){
        txtID.setText("");
        txtName.setText("");
        txtDateCreate.setText(DateUtility.today());
        txtDecription.setText("");
    }
   public boolean checkEmpty()
    {
        boolean test = true;
        if(txtID.getText().equals(""))
        {
            test = false;
        }
        if(txtName.getText().equals(""))
        {
            test = false;
        }
        if(txtDecription.getText().equals(""))
        {
            test = false;
        }
        return test;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnBackground;
    private javax.swing.JPanel pnContent;
    private javax.swing.JPanel pnProduct;
    private javax.swing.JScrollPane srDecription;
    private javax.swing.JTextField txtDateCreate;
    private javax.swing.JTextPane txtDecription;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        Object obj = e.getSource();
    	for(int i = 0; i < product.length; i++){
    		if(obj.equals(product[i])){
                    displayObject(i);
    		}
    	}
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

}
